package com.cqupt.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.cqupt.service.DemoUtilizationService;
import com.cqupt.utils.APIUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* @Description //获取相关利用率的历史数据
* @Date 20:13 2020/12/6
* @Param
**/
@Service
@Transactional
public class DemoUtilizationServiceImpl implements DemoUtilizationService {
    /**
    * @Description //通过name如CPU utilization或memory utilization获取CPU以及内存利用率的历史数据
    * @Date 20:15 2020/12/6
    * @Param [hostId, name]
    **/
    @Override
    public List<Map<String, Object>> getUtilizationByName(String hostId,String name) {
        String token = APIUtils.getToken();
        List<Map<String, Object>> itemList = getItemsByName(token, hostId, name);
        String itemId = itemList.get(0).get("itemid").toString();
        List<Map<String, Object>> historyDataList = getItemHistoryData(token, itemId, "clock", "DESC", 20);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        for(Map<String, Object> historyData : historyDataList) {
            String clock = sdf.format(new Date(Long.valueOf(historyData.get("clock").toString()) * 1000L));
            historyData.put("clock", clock);
        }
        return historyDataList;
    }
    /**
    * @Description //通过hostid和name寻找CPU或内存对应的监控项
    * @Date 20:16 2020/12/6
    * @Param [token, hostId, name]
    **/
    public List<Map<String, Object>> getItemsByName(final String token, final String hostId, final String name) {
        Map<String, Object> paramsMap = new HashMap<String, Object>(){{
            put("jsonrpc", "2.0");
            put("method", "item.get");
            put("id", 1);
            put("auth", token);
            put("params", new HashMap<String, Object>(){{
                put("output", "extend");
                put("hostids", hostId);
                put("search", new HashMap<String, Object>(){{
                    put("name", name);
                }});
                put("sortfield", "name");
            }});
        }};
        Map<String, Object> responseMap = APIUtils.sendRequest(JSON.toJSONString(paramsMap, SerializerFeature.WriteMapNullValue));
        List<Map<String, Object>> resultList = (List<Map<String, Object>>)responseMap.get("result");
        return resultList;
    }
/**
* @Description //通过监控项id查找对应的历史数据
* @Date 20:17 2020/12/6
* @Param [token, itemId, sortfield, sortorder, limit]
**/
    public List<Map<String, Object>> getItemHistoryData(final String token, final String itemId, final String sortfield, final String sortorder, final Integer limit) {
        Map<String, Object> paramsMap = new HashMap<String, Object>(){{
            put("jsonrpc", "2.0");
            put("method", "history.get");
            put("id", 1);
            put("auth", token);
            put("params", new HashMap<String, Object>(){{
                put("output", "extend");
                put("history", 0);
                put("itemids", itemId);
                put("sortfield", sortfield);
                put("sortorder", sortorder);
                put("limit", limit);
            }});
        }};
        Map<String, Object> responseMap = APIUtils.sendRequest(JSON.toJSONString(paramsMap, SerializerFeature.WriteMapNullValue));
        List<Map<String, Object>> resultList = (List<Map<String, Object>>)responseMap.get("result");
        return resultList;
    }
}
